package com.obviousframework.mom.service.impl;

import java.util.UUID;

import javax.ejb.Stateless;
import javax.jws.WebService;

import com.obviousframework.mom.model.Message;
import com.obviousframework.mom.model.Tag;
import com.obviousframework.mom.service.Messenger;
import com.obviousframework.mom.taghandler.TagHandler;
import com.obviousframework.mom.taghandler.TagHandlerFactory;

@Stateless
@WebService(name = "Messenger")
public class MessengerImpl implements Messenger {

	public String send(Message message) {

		if (message == null) {
			return null;
		}

		String id = getNextId();
		message.setId(id);

		for (Tag tag : message.getTags()) {
			TagHandler handler = TagHandlerFactory.getHandler(tag);
			if (handler != null) {
				handler.process(message);
			}
		}
		return id;
	}

	private String getNextId() {
		return UUID.randomUUID().toString();
	}
}
